package com.luxury.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.luxury.R;
import com.luxury.fragment.sellementcenter_fragment.AddressFragment;
import com.luxury.fragment.sellementcenter_fragment.BillFragment;
import com.luxury.fragment.sellementcenter_fragment.DispatchFragment;
import com.luxury.fragment.sellementcenter_fragment.ExchangeFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CenterToActivity extends AppCompatActivity {

    private static final String TAG = "centerto";
    @BindView(R.id.framgent_layout_centerTo)
    FrameLayout framgent_Layout;
    @BindView(R.id.iv_return_centerTo)
    ImageView iv_Return;
    @BindView(R.id.tv_title_centerTo)
    TextView tv_Title;
    @BindView(R.id.tv_activition_centerTo)
    TextView tv_Activition;
    private FragmentManager mManager;
    private List<Fragment> mList_fragment = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_center_to);
        ButterKnife.bind(this);
        mManager = getSupportFragmentManager();
        String title = getIntent().getStringExtra("title");
        int index = getIntent().getIntExtra("index", 0);
        if (index == 3) {
            if (tv_Activition.getVisibility()==View.GONE){
                tv_Activition.setVisibility(View.VISIBLE);
            }
        }else {
            if (tv_Activition.getVisibility()==View.VISIBLE){
                tv_Activition.setVisibility(View.GONE);
            }
        }
        tv_Title.setText(title);
        iv_Return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        initFragment(index);

    }

    private void initFragment(int index) {
        AddressFragment address_fragment = AddressFragment.newInstance();
        BillFragment bill_fragment = BillFragment.newInstance();
        DispatchFragment dispatch_fragment = DispatchFragment.newInstance();
        ExchangeFragment exchage_fragment = ExchangeFragment.newInstance();
        mList_fragment.add(dispatch_fragment);
        mList_fragment.add(address_fragment);
        mList_fragment.add(exchage_fragment);
        mList_fragment.add(bill_fragment);
        switchFragment(mManager, mList_fragment, index, R.id.framgent_layout_centerTo);
    }

    //点击激活时候的方法
    public void onActivition(View view) {

    }

    //-------------------(封装方法)选择fragment的方法(fragment 都加在出来  然后不用的隐藏)----------------------------
    private void switchFragment(FragmentManager manager, List<Fragment> list_fragment, int
            tabIndex, @IdRes int layoutId) {
        FragmentTransaction transaction = manager.beginTransaction();
        //       循环遍历吧所有的fragment隐藏起来
        for (int i = 0; i < list_fragment.size(); i++) {
            if (list_fragment.get(i).isVisible()) {
                transaction.hide(list_fragment.get(i));
            }
        }
        //        判断需要哪个fragment  然后如果没有的 将这个fragment添加 如果有的 将fragment显示出来
        Log.i(TAG, "点击下标-->>" + tabIndex);
        Fragment fragment = list_fragment.get(tabIndex);
        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.add(layoutId, fragment);
        }
        transaction.commit();
    }


}

